package fac;

import static org.junit.Assert.assertTrue;
import robocode.Rules;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that ThirdRobotLaw has fired at least one max powered bullet against SittingDuck. This is
 * possible due to consecutive hits warranting adjustment of bullet power to maximum.
 * 
 * @author Chris Fang
 */
public class TestTRLRepeatedStrikes extends RobotTestBed {

  // True if ThirdRobotLaw used maxmium bullet power
  boolean powerMaxed = false;

  /**
   * Initiates the battle with SittingDuck and ThirdRobotLaw as the participants.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,fac.ThirdRobotLaw";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * At the end of each turn, checks the power of all bullets moving across the battlefield. Check
   * if any of them are using the maximum bullet power.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // All active bullets belong to ThirdRobotLaw since SittingDuck does not fire.
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();

    for (int i = 0; i < bullets.length; i++) {

      if (bullets[i].getPower() == Rules.MAX_BULLET_POWER) {
        powerMaxed = true;
      }
    }
  }

  /**
   * After running all matches, determine if ThirdRobotLaw was scaling its bullet power.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Bullet power reached maximum", powerMaxed);
  }
}